package cz.zcu.fav.rat.dataaccess.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cz.zcu.fav.rat.dataaccess.dao.ObjectDAO;
import cz.zcu.fav.rat.dataaccess.dao.UserDAO;
import cz.zcu.fav.rat.dataaccess.service.ObjectService;
import cz.zcu.fav.rat.beans.Object;
import cz.zcu.fav.rat.beans.User;

@Service("objectService")
@Transactional
public class ObjectServiceImpl extends
		GenericDataServiceImpl<Object, ObjectDAO> implements ObjectService
{
	@Autowired
	UserDAO userDAO;

	@Autowired
	public ObjectServiceImpl(ObjectDAO dao)
	{
		super(dao);
	}

	@Override
	@Transactional(readOnly = false)
	public void delete(Object persistentObject)
	{
		List<User> users = userDAO.findUsersByFavouriteId(persistentObject
				.getId());
		for (User user : users)
		{
			user.getFavourites().remove(persistentObject);
			
			userDAO.update(user);
		}
		persistentObject = merge(persistentObject);
		super.delete(persistentObject);
	}

	@Override
	public List<Object> findAllObjectsByIdentifier(String identifier)
	{
		return dao.findObjectByIdentifier(identifier);
	}
}
